Calling an External Function
This mechanism can be used to call an external function in an IEC application via a Unix Domain Socket. This allows you to create your own functionalities. At the same time, process separation is achieved. As an interface in IEC, the EXTAPI.UDSExternalFunction
function block is provided in the ExtensionAPI
library. In Python, the ExternalFunctionBase
class is used for this purpose, which is provided in the supplied example.
This example also shows how to use the mechanism. The example contains an IEC project with implemented call of an external function. In addition, the example contains the implementation of the called function myExternalFunction
in Python.
After CODESYS Control Extension Package is installed, you can find the UDSExternalCallExample.project
example and the Python script uds_external_function.py
in the following directory: C:\Program Files\CODESYS <version>\CODESYS\CODESYS Control SL Extension Package\<version>\Examples\ExternalCall
.
Example, IEC application
In the UDSExternalCallExample.project
sample project, a new myExternalFunction
function block is created. This extends the EXTAPI.UDSExternalFunction
FB with inputs and outputs of type DINT
. Other data types are also possible. At the beginning, the name of the function to be called in Python needs to be passed. This address must be unique.
The following code is used to add the inputs of the external function of the interface:
Result := THIS^.AddParameter('parameterIn1', 'DINT', TO_STRING(diIn1));
The following code is then used to call the external function:
Result := THIS^.Call();
The following code is used to query the return value of the function implemented in Python:
Result := THIS^.GetParameter('parameterOut', 'DINT', ADR(sValue));
The parameters are of type string
. Accordingly, it has to be cast to the desired data type.
Example, Python script
Important
The provided Python examples for the Extension API require Python 3.0.
The included uds_external_function.py
sample provides the ExternalFunctionBase
class, which allows Unix Domain Socket communication with the runtime system for this functionality.
The myExternalFunction
class implemented in the example extends the ExternalFunctionBase
class. The Call()
function is required, which is called from the mechanism from IEC. Input and output parameters are passed in the form of tuples of strings.
Example: {'parameterIn1': ('DINT', '11'), 'parameterIn2': ('DINT', '22')}
In the example, the Call()
function implements a simple addition of the two input parameters:
def Call(self, dictParams): in1 = int(dictParams['parameterIn1'][1]) in2 = int(dictParams['parameterIn2'][1]) dictRetParams = {} dictRetParams['parameterOut'] = 'DINT', in1 + in2 return dictRetParams
Copy the script
uds_external_function.py
(located in the directoryC:\Program Files\CODESYS <version>\CODESYS\CODESYS Control SL Extension Package\<version>\Examples\ExternalCall
after CODESYS Control Extension Package is installed) to the target device.Run the
uds_external_function.py
script.Then start the
UDSExternalCallExample.project
sample project in CODESYS.You can see that the project calls the
myExternalFunction()
function of the Python script and the two input parameters are added.
Usage of the interface only as a member of the Linux | Yes |
Process separation | Yes |